/**
  ******************************************************************************
  * @file    usart.c
  * @brief   This file provides code for the configuration
  *          of the USART instances.
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "bsp.h"

/* USART1 init function */
void SystemUSART_Config(void)
{
  LL_USART_InitTypeDef USART_InitStruct = {0};
  LL_GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* Peripheral clock enable */
  LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_USART1);
  LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOC);
	
  /**USART1 GPIO Configuration
  PC4   ------> USART1_TX
  PC5   ------> USART1_RX
  */
  GPIO_InitStruct.Pin = LL_GPIO_PIN_4;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
  GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
  GPIO_InitStruct.Alternate = LL_GPIO_AF_1;
  LL_GPIO_Init(GPIOC, &GPIO_InitStruct);

  GPIO_InitStruct.Pin = LL_GPIO_PIN_5;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
  GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
  GPIO_InitStruct.Alternate = LL_GPIO_AF_1;
  LL_GPIO_Init(GPIOC, &GPIO_InitStruct);

  /* USART1 interrupt Init */
  NVIC_SetPriority(USART1_IRQn, 3);
  NVIC_EnableIRQ(USART1_IRQn);

  USART_InitStruct.PrescalerValue = LL_USART_PRESCALER_DIV1;
  USART_InitStruct.BaudRate = 115200;
  USART_InitStruct.DataWidth = LL_USART_DATAWIDTH_8B;
  USART_InitStruct.StopBits = LL_USART_STOPBITS_1;
  USART_InitStruct.Parity = LL_USART_PARITY_NONE;
  USART_InitStruct.TransferDirection = LL_USART_DIRECTION_TX;//LL_USART_DIRECTION_TX_RX;
  USART_InitStruct.HardwareFlowControl = LL_USART_HWCONTROL_NONE;
  USART_InitStruct.OverSampling = LL_USART_OVERSAMPLING_16;
  LL_USART_Init(USART1, &USART_InitStruct);
	
	LL_USART_ConfigAsyncMode(USART1);
	
  LL_USART_SetTXFIFOThreshold(USART1, LL_USART_FIFOTHRESHOLD_1_8);
  LL_USART_SetRXFIFOThreshold(USART1, LL_USART_FIFOTHRESHOLD_1_8);
  LL_USART_DisableFIFO(USART1);
	
  LL_USART_SetRxTimeout(USART1, 5760u);                         /* (1/115200)  50ms */
  LL_USART_EnableRxTimeout(USART1);
	
	LL_USART_SetDEAssertionTime(USART1, 31);                      /* 485方向控制脚的时间 */
  LL_USART_SetDEDeassertionTime(USART1, 31);                    /* 485方向控制脚的时间 */
  LL_USART_SetDESignalPolarity(USART1, LL_USART_DE_POLARITY_HIGH);
  LL_USART_DisableDEMode(USART1);
	
	LL_USART_DisableDMADeactOnRxErr(USART1);
  
  LL_USART_DisableInStopMode(USART1);                           /* 低功耗模式下唤醒mcu: disable */
  
	LL_USART_SetWakeUpMethod(USART1, LL_USART_WAKEUP_IDLELINE);   /* uart唤醒方法:空闲线路和地址匹配 */
  LL_USART_DisableMuteMode(USART1);                             /* 关闭uart唤醒功能,始终处于工作模式,若为唤醒模式,则根据上面的唤醒方法 */
  
  LL_USART_SetTXRXSwap(USART1, LL_USART_TXRX_STANDARD);         /* tx rx引脚交换:标准 */
  
  LL_USART_SetTXPinLevel(USART1, LL_USART_TXPIN_LEVEL_STANDARD);
  LL_USART_SetRXPinLevel(USART1, LL_USART_RXPIN_LEVEL_STANDARD);
  LL_USART_SetBinaryDataLogic(USART1, LL_USART_BINARY_LOGIC_POSITIVE);
  
  LL_USART_DisableAutoBaudRate(USART1);
  LL_USART_DisableOneBitSamp(USART1);                           /* 禁止单位采样,采用3位采样 */
  
  LL_USART_DisableHalfDuplex(USART1);
  
  LL_USART_EnableIT_RXNE_RXFNE(USART1);
  LL_USART_EnableIT_RTO(USART1);
	
  LL_USART_Enable(USART1);

  /* Polling USART1 initialisation */
  while((!(LL_USART_IsActiveFlag_TEACK(USART1))))//|| (!(LL_USART_IsActiveFlag_REACK(USART1))
  {
  }
	
	LL_USART_ClearFlag_RTO(USART1);
}

extern void Terminal_RecvIsr(uint8_t dat);
extern void Terminal_BreakIsr(void);

void USART1_IRQHandler(void)
{
	if(READ_BIT(USART1->ISR, USART_ISR_RXNE_RXFNE) != 0)
	{
//		Terminal_RecvIsr(USART1->RDR);
	}
  
  if(READ_BIT(USART1->ISR, USART_ISR_RTOF) != 0)
	{
    LL_USART_ClearFlag_RTO(USART1);
    
//		Terminal_BreakIsr();
  }
}

/* printf */

FILE __stdout;

int fputc(int ch, FILE *f)
{
  USART1->TDR = ( (uint8_t)ch & (uint16_t)0x01FF );
  while(READ_BIT(USART1->ISR, USART_ISR_TXE_TXFNF) != (USART_ISR_TXE_TXFNF));

  return (ch);
}

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
